﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Vehicle.Web.Pages.Pilot
{
   /// <summary>
/// Copyright (C) M-Tear团队
/// 文 件 名：Pilot_Add.aspx.cs
/// 版    本：v1.00.0000
/// 创建标识：2009-08-30   Created by 杨光远 V1.00.0000
/// 功能说明：添加驾驶员页面类
/// 注意事项：无
/// 
/// 更新记录：
/// 
/// </summary> 
    public partial class Pilot_Add : Component.PageBase
    {
        #region ---基本变量实例化---
        Vehicle.BLL.Pilot b_Pilot = new Vehicle.BLL.Pilot();//定义逻辑层变量
         BLL.Log b_Log = new Vehicle.BLL.Log();
        Vehicle.Model.PilotInfo m_PilotInfo = new Vehicle.Model.PilotInfo();//定义实体层变量
        Vehicle.BLL.Vehicles b_Vehicles = new Vehicle.BLL.Vehicles();//定义逻辑层变量
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        #endregion

        #region ---基本方法---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MotorcadeDataBind();//绑定数据
            }
        }

        /// <summary>
        /// 初始绑定车队下拉数据
        /// </summary>
        public void MotorcadeDataBind()
        {
            b_Motorcade.DropDownListBind(drop_M_ID);//绑定车队
            VehiclesDataBind();//绑定车辆
        }
        /// <summary>
        /// 根据车队下拉菜单绑定下属车辆
        /// </summary>
        public void VehiclesDataBind()
        {
            b_Vehicles.DropDownListBind(drop_V_ID, new Vehicle.Model.ComponentsInfo("Vehicles", "M_ID", drop_M_ID.SelectedValue, "P_ID", "0", "V_IsDelete", "1", "V_ID"));
        }
        /// <summary>
        /// 添加驾驶员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            //添加前判断是否已经有该编号的驾驶员
            if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Pilot", "P_IsDelete", "0", "P_Num",txt_P_Num.Text.Trim(), "P_ID")).Rows.Count == 0)
            {
                m_PilotInfo.M_ID = Common.TextHelper.StringToInt(drop_M_ID.SelectedValue);//所属车队ID
                m_PilotInfo.V_ID = Common.TextHelper.StringToInt(drop_V_ID.SelectedValue);//所属车辆
                m_PilotInfo.P_Num = txt_P_Num.Text.Trim();//驾驶员编号
                m_PilotInfo.P_Age = Common.TextHelper.StringToInt(txt_P_Age.Text.Trim());//驾驶员年龄
                m_PilotInfo.P_CarAge = Common.TextHelper.StringToInt(txt_P_CarAge.Text.Trim());// 驾驶员车龄
                m_PilotInfo.P_Sex = Common.TextHelper.StringToInt(drop_P_Sex.SelectedValue);//驾驶员性别
                m_PilotInfo.P_License = txt_P_License.Text.Trim();//驾驶员驾照
                m_PilotInfo.P_Name=txt_P_Name.Text.Trim();//驾驶员姓名
                m_PilotInfo.P_Tel=txt_P_Tel.Text.Trim();//联系电话
                m_PilotInfo.P_Address = txt_P_Address.Text.Trim();//联系地址
                m_PilotInfo.P_CardID = txt_P_CardID.Text.Trim();//驾驶员身份证
                m_PilotInfo.P_Transit = 0;//是否有任务 默认0 则没有
                m_PilotInfo.P_Note = txt_P_Note.Text.Trim();//驾驶员备注
                m_PilotInfo.P_LastTime = DateTime.Now;//最后修改时间 默认为当前时间
                m_PilotInfo.P_IsDelete = 0;//是否删除 默认0 则不删除
                m_PilotInfo.P_Time = DateTime.Now;//录入时间 默认为当前时间
                try
                {
                    b_Pilot.Add(m_PilotInfo);
                    string   p_id =b_Pilot.GetModel(new Vehicle.Model.ComponentsInfo("Pilot", "P_Num", txt_P_Num.Text.Trim(), "P_ID")).Rows[0]["P_ID"].ToString();//查出刚刚添加的记录
                    b_Pilot.AllUpdate(new Vehicle.Model.ComponentsInfo ("Vehicles","P_ID",p_id,"V_ID",drop_V_ID.SelectedValue));//更改车辆表
                    b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功添加编号为" +txt_P_Num.Text.Trim() + "的驾驶员信息信息"));//添加日志
                    Common.MessageHelper.AlertRedirect("驾驶员信息添加成功", "Main.aspx");
                }
                catch
                {
                     Common.MessageHelper.Alert("添加数据失败!");
                }               
            }
            else
            {
                Common.MessageHelper.Alert("已经有相同该编号的驾驶员,请重新填写");
            }
        }
        /// <summary>
        /// 车队选择绑定下属车辆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            VehiclesDataBind();//绑定车辆
        }
        #endregion
    }
}
